package com.itheima.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.itheima.common.PageInfo;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Employee;
import com.itheima.service.CategoryService;
import com.itheima.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YangJiaHao
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public boolean save(Category category) {
        Snowflake snowflake = IdUtil.getSnowflake();
        String id = snowflake.nextIdStr();
        category.setId(id);
        Integer res = categoryMapper.save(category);
        return res>0;
    }

    @Override
    public List<Category> getList(Integer type){
        List<Category> list = categoryMapper.getList(type);
        return list;
    }

    @Override
    public PageInfo<Category> page(Integer page, Integer pageSize) {
        PageInfo<Category> pageInfo = new PageInfo<>();
        Integer start = (page-1) * pageSize;
        List<Category> categories = categoryMapper.page(start, pageSize);
        pageInfo.setRecords(categories);
        pageInfo.setSize(pageSize);
        pageInfo.setCurrent(page);
        pageInfo.setTotal(categoryMapper.count());
        return pageInfo;
    }

    @Override
    public boolean delete(String id) {
        Integer res = categoryMapper.delete(id);
        return res>0;
    }

    @Override
    public boolean update(Category category) {
        Integer res = categoryMapper.update(category);
        return res>0;
    }

    @Override
    public List<CategoryVo> list() {
        List<Category> categories = categoryMapper.selectList();
        List<CategoryVo> categoryVos = new ArrayList<>();
        for (Category category : categories) {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            categoryVos.add(categoryVo);
        }
        return categoryVos;
    }
}
